import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/clockin/attendances.dart';
import 'package:app/src/http/bean/http_data.dart';

import '../../../config/app_data_config.dart';
import '../../../manager/user_manager.dart';
import '../../../manager/ywx_manager.dart';
import '../works_2023/purcontract_bean.dart';
import '../wroks/contract_bean.dart';
import '../wroks/search_bean.dart';
import '../ywx_limit.dart';
import 'package:decimal/decimal.dart';

///设备管理：我的工单：工单领用：五金领用

// {"Res":true,"MsgCode":"100","MsgDetail":"","Data":{"01":"办公用品","02":"劳保用品","03":"设备配件","04":"包装材料","05":"小型工具","06":"五金材料","07":"其他","08":"学习用品"},"TotalRowCount":0}
///五金类型实体类参数
class HardwareTypeBean extends BaseBean {
  List<RollerBean>? datas = [];

  @override
  fromJson(Map<String, dynamic> json) {
    //print("进来了：\t$json");
    //     datas?.clear();
    if (datas == null) {
      datas = [];
    }
    json?.forEach((key, value) {
      datas?.add(RollerBean(key: key, value: value));
    });
  }

  @override
  instance() {
    // TODO: implement instance
    return HardwareTypeBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    // map["Code"] = Code ?? "";
    // map["PageIndex"] = PageIndex ?? "";
    return map;
  }
}

// {
// "#IdIndex": 1,
// "PackageNum": 4,
// "sumBasicNum": 2.000000,
// "name": "一次性杯子",
// "Id": "WJ41917",
// "Size": "5cm*8cm*0.5cm",
// "BasicUnit": "包",
// "ManufacturerId": 51,
// "ManufacturerName": "007牌",
// "basicNum": 1.000000,
// "packageunit": "",
// "ItemName": "办公用品",
// "StoreHouseType": "03",
// "code": "c12",
// "EquipmentName": "",
// "EquipmentCode": "",
// "note": "111"
// }
///五金列表实体类
class HardwareBean extends BaseBean {
  String? PackageNum = "";

  //sumBasicNum 库存剩余数量;
  String? sumBasicNum = "";

  //sumBasicNum2 fixme 自己定义的领用数量
  String? sumBasicNum2 = "";

  //获取领用数量
  double getsumBasicNum2() {
    if (StringUtils.isEmpty(sumBasicNum2)) {
      return 0;
    }
    try {
      return double.parse(sumBasicNum2!);
    } catch (e) {}
    return 0;
  }

  ///获取文本款输入最大值。
  double getMax() {
    if (StringUtils.isEmpty(sumBasicNum)) {
      return 0;
    }
    return double.parse(sumBasicNum!);
  }

  String? name = "";
  String? Id = "";
  String? Size = "";

  //BasicUnit 基本单位
  String? BasicUnit = "";
  String? ManufacturerId = "";
  String? ManufacturerName = "";

  //basicNum 基本数量
  String? basicNum = "";
  String? packageunit = "";
  String? ItemName = "";
  String? StoreHouseType = "";

  //code 仓位号
  String? code = "";
  String? EquipmentName = "";
  String? EquipmentCode = "";
  String? note = "";

  ///单位数量
  String getUnitNum() {
    return "${basicNum}${BasicUnit}/${packageunit}";
  }

  ///仓库剩余数量
  String getsumBasicNum() {
    return "$sumBasicNum$BasicUnit";
  }

  // DefUnit；0-基本单位 1-包装单位
  String? DefUnit = "";

  String? DefUnitNum=""; //自己加的字段；用于记录用户手动输入的领用数量。

  ///fixme 根据领用数量，自动计算实际领用数量
  void auotSumBasicNum2() {
    if (isBaseUnit()) {
      //基本单位领用
      sumBasicNum2 = DefUnitNum;
    } else {
      //包装单位领用
      try {
        sumBasicNum2 = DecimalUtils.multiply(DefUnitNum, basicNum);
      } catch (e) {}
    }
  }

  ///fixme 根据实际领用数量，自动计算领用数量
  void auotDefUnitNum() {
    if (isBaseUnit()) {
      //基本单位领用
      DefUnitNum = sumBasicNum2;
    } else {
      //包装单位领用
      try {
        //领用数量/每包多少数量=包装单位数量。
        DefUnitNum = DecimalUtils.divide(sumBasicNum2, basicNum);
      } catch (e) {}
    }
  }

  ///获取系统配置的领用单位。
  String? getDefUnitName() {
    if (DefUnit?.trim() == "1") {
      return packageunit; //包装单位
    }
    return BasicUnit; //基本单位。
  }

  ///判断领用数量，是否以基本单位为准。
  bool isBaseUnit() {
    if (DefUnit?.trim() == "1") {
      return false; //包装单位
    }
    return true; //基本单位。
  }

  String? _DefUnitMax = null;

  ///获取领用数量最大值，根据领用单位来。
  String? getDefUnitMax() {
    if (StringUtils.isEmpty(_DefUnitMax)) {
      if (isBaseUnit()) {
        _DefUnitMax = sumBasicNum; //基本单位就是库存剩余数量。
      } else {
        //最大包数量=库存数量/单位数量（即：每包多少数量）
        try {
          _DefUnitMax = DecimalUtils.divide(sumBasicNum, basicNum);
        } catch (e) {}
      }
    }
    return _DefUnitMax;
  }

  // String get

  @override
  fromJson(Map<String, dynamic> json) {
    PackageNum = json["PackageNum"]?.toString() ?? "";
    sumBasicNum = json["sumBasicNum"]?.toString() ?? "";
    name = json["name"]?.toString() ?? "";
    Id = json["Id"]?.toString() ?? "";
    Size = json["Size"]?.toString() ?? "";
    BasicUnit = json["BasicUnit"]?.toString() ?? "";
    ManufacturerId = json["ManufacturerId"]?.toString() ?? "";
    ManufacturerName = json["ManufacturerName"]?.toString() ?? "";
    basicNum = json["basicNum"]?.toString() ?? "";
    packageunit = json["packageunit"]?.toString() ?? "";
    ItemName = json["ItemName"]?.toString() ?? "";
    StoreHouseType = json["StoreHouseType"]?.toString() ?? "";
    code = json["code"]?.toString() ?? "";
    EquipmentName = json["EquipmentName"]?.toString() ?? "";
    EquipmentCode = json["EquipmentCode"]?.toString() ?? "";
    note = json["note"]?.toString() ?? "";
    DefUnit = json["DefUnit"]?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return HardwareBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["PackageNum"] = PackageNum ?? "";
    map["sumBasicNum"] = sumBasicNum ?? "";
    map["name"] = name ?? "";
    map["Id"] = Id ?? "";
    map["Size"] = Size ?? "";
    map["BasicUnit"] = BasicUnit ?? "";
    map["ManufacturerId"] = ManufacturerId ?? "";
    map["ManufacturerName"] = ManufacturerName ?? "";
    map["basicNum"] = basicNum ?? "";
    map["packageunit"] = packageunit ?? "";
    map["ItemName"] = ItemName ?? "";
    map["StoreHouseType"] = StoreHouseType ?? "";
    map["code"] = code ?? "";
    map["EquipmentName"] = EquipmentName ?? "";
    map["EquipmentCode"] = EquipmentCode ?? "";
    map["note"] = note ?? "";
    map["DefUnit"] = DefUnit ?? "";

    ///五金领用提交参数
    // "outnum": "1",
    // "matercode": "WJ41917",
    // "materialName": "一次性杯子",
    // "materialUnit": "包",
    // "materialNum": "1.000000",
    // "materialBasicNum": "14.000000",
    // "materialPickBodyId": "",
    // "materialManufacturerId": "51",
    // "materialManufacturerName": "007牌",
    // "materialBasicNum1": "1.000000",
    // "materialPackageUnit": "",
    // "materialEquipmentCode": "",
    // "materialSize": "5cm*8cm*0.5cm",
    // "materialpositon": "S2304",
    // "materialEquipmentName": "",
    // "note": ""
    map["outnum"] = sumBasicNum2 ?? ""; //fixme 实际领用数量
    map["matercode"] = Id ?? "";
    map["materialName"] = name ?? "";
    map["materialUnit"] = BasicUnit ?? "";
    map["materialNum"] = basicNum ?? "";
    map["materialBasicNum"] = sumBasicNum ?? "";
    map["materialPickBodyId"] = "";
    map["materialManufacturerId"] = ManufacturerId ?? "";
    map["materialManufacturerName"] = ManufacturerName ?? "";
    map["materialBasicNum1"] = basicNum ?? "";
    map["materialPackageUnit"] = packageunit ?? "";
    map["materialEquipmentCode"] = EquipmentCode ?? "";
    map["materialSize"] = Size ?? "";
    map["materialpositon"] = code ?? "";
    map["materialEquipmentName"] = EquipmentName ?? "";
    map["note"] = note ?? "";
    map["DefUnit"] = DefUnit ?? "";
    return map;
  }
}

///五金领用提交数据
class SubmitHardwareReceive extends BaseBean {
  String? RepairOrderId = ""; //工单号
  String? FactoryId = ""; //工厂id
  List<HardwareBean>? ItemInfo;
  bool IsConfirm = false; //是否二次确认提交

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
  }

  @override
  instance() {
    // TODO: implement instance
    return SubmitHardwareReceive();
  }

  @override
  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["RepairOrderId"] = RepairOrderId ?? "";
    map["FactoryId"] = FactoryId ?? "";
    map["ItemInfo"] = ItemInfo;
    map["IsConfirm"] = IsConfirm ?? false;
    return map;
  }
}
